package com.gg.haoqi.utils;


import android.content.Context;
import android.os.Build;
import android.os.Environment;

import java.io.File;

public class FileUtil {

    /**
     * 获取程序APP根目录
     *
     * @return
     */


    public static File getRootCache(Context context) {
        File rootFile = null;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
            rootFile = context.getExternalCacheDir();
        } else {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                rootFile = new File(Environment.getExternalStorageDirectory() + File.separator + AppConstant.ROOT_CACHE);
            } else {
                rootFile = new File(Environment.getDataDirectory() + File.separator + context.getPackageName());
            }
            if (!rootFile.exists()) {
                rootFile.mkdirs();
            }
        }
        return rootFile;
    }

    /**
     * 获取图片的保存路径
     *
     * @return
     */
    public static File getImageCache(Context context) {
        String imageCache = getRootCache(context) + File.separator + AppConstant.APP_IMAGE_CACHE;
        File file = new File(imageCache);
        if (!file.exists()) {
            file.mkdir();
        }
        return file;
    }


    //获取数据库保存路径
    public static File getDbFile(Context context) {
        String dbPath = getRootCache(context) + File.separator + AppConstant.DB_DIR;
        File dbFile = new File(dbPath);
        if (!dbFile.exists()) {
            dbFile.mkdir();
        }
        return dbFile;
    }

}
